﻿using Learun.Util;
using Learun.Util.Constant;
using System;
using System.Collections.Generic;

namespace Learun.Application.WorkFlow
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创建人：研发部
    /// 日 期：2018.12.06
    /// 描 述：工作流模板(新)
    /// </summary>
    public class NWFSchemeBLL : NWFSchemeIBLL
    {
        private NWFSchemeService nWFSchemeService = new NWFSchemeService();

        #region 获取数据
        /// <summary>
        /// 获取流程分页列表
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="queryJson">查询条件</param>
        /// <returns></returns>
        public IEnumerable<NWFSchemeInfoEntity> GetInfoPageList(Pagination pagination, string queryJson)
        {
            return nWFSchemeService.GetInfoPageList(pagination, queryJson);
        }
        /// <summary>
        /// 获取自定义流程列表
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        /// <returns></returns>
        public IEnumerable<NWFSchemeInfoEntity> GetInfoList(UserInfo userInfo, string ProjectId)
        {
            return nWFSchemeService.GetInfoList(userInfo, ProjectId);
        }

        /// <summary>
        /// 获取流程模板列表 
        /// </summary>
        /// <param name="categoryId">分类Id</param>
        /// <returns></returns>
        public IEnumerable<NWFSchemeInfoEntity> GetInfoListByCategoryId(string categoryId)
        {
            return nWFSchemeService.GetInfoListByCategoryId(categoryId);
        }

        /// <summary>
        ///  获取流程分类树形数据
        /// </summary>
        /// <param name="ProjectId">项目ID</param>
        /// <returns></returns>
        public List<TreeModel> GetCategoryTree(string ProjectId, string PlantType)
        {

            List<string> clist = new List<string>() { };
            List<TreeModel> treeList = new List<TreeModel>();
            List<NWFSchemeCategoryEntity> list = nWFSchemeService.GetCategoryTree(ProjectId, PlantType);
            foreach (var item in list)
            {
                TreeModel node = new TreeModel
                {
                    id = item.FlowClassifyID,
                    text = item.ClassifyName,
                    value = item.FlowClassifyID,
                    showcheck = false,
                    checkstate = 0,
                    isexpand = true,
                    parentId = ""
                };
                treeList.Add(node);
            }
            return treeList.ToTree();

        }

        /// <summary>
        /// 获取流程分类数据
        /// </summary>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        public List<NWFSchemeCategoryEntity> GetCategoryList(string queryJson)
        {

            return nWFSchemeService.GetCategoryList(queryJson);
        }

        /// <summary>
        /// 获取流程分类数据
        /// </summary>
        /// <param name="plantType"> 流程分类00001单位平台，00002项目平台</param>
        /// <param name="ProjectId"> 项目ID</param>
        /// <returns></returns>
        public List<NWFSchemeCategoryEntity> GetNWFSchemeCategoryEntityList(string plantType, string ProjectId)
        {
            return nWFSchemeService.GetNWFSchemeCategoryEntityList(plantType, ProjectId);
        }

        /// <summary>
        /// 获取流程分类实体
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        public NWFSchemeCategoryEntity GetCategoryForm(string keyValue)
        {
            return nWFSchemeService.GetCategoryForm(keyValue);

        }

        /// <summary>
        /// 获取流程列表
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        /// <returns></returns>
        public IEnumerable<NWFSchemeInfoEntity> GetInfoList()
        {
            return nWFSchemeService.GetInfoList();
        }


        /// <summary>
        /// 获取流程模板分页列表
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="userInfo">登录者信息</param>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        public IEnumerable<NWFSchemeInfoEntity> GetAppInfoPageList(Pagination pagination, UserInfo userInfo, string queryJson)
        {
            return nWFSchemeService.GetAppInfoPageList(pagination, userInfo, queryJson);
        }

        /// <summary>
        /// 获取模板基础信息的实体
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        public NWFSchemeInfoEntity GetInfoEntity(string keyValue)
        {
            return nWFSchemeService.GetInfoEntity(keyValue);
        }
        /// <summary>
        /// 获取模板基础信息的实体
        /// </summary>
        /// <param name="code">流程编号</param>
        /// <returns></returns>
        public NWFSchemeInfoEntity GetInfoEntityByCode(string code)
        {
            return nWFSchemeService.GetInfoEntityByCode(code);
        }
        /// <summary>
        /// 获取模板基础信息的实体
        /// </summary>
        /// <param name="code">流程编号</param>
        /// <param name="ProjectId">项目ID</param>
        /// <returns></returns>
        public NWFSchemeInfoEntity GetInfoEntityByCodeAndProjectId(string code, string ProjectId)
        {
            return nWFSchemeService.GetInfoEntityByCodeAndProjectId(code, ProjectId);
        }

        /// <summary>
        /// 获取流程模板权限列表
        /// </summary>
        /// <param name="schemeInfoId">模板信息主键</param>
        /// <returns></returns>
        public IEnumerable<NWFSchemeAuthEntity> GetAuthList(string schemeInfoId)
        {
            return nWFSchemeService.GetAuthList(schemeInfoId);
        }

        /// <summary>
        /// 获取模板列表
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="schemeInfoId">流程信息主键</param>
        /// <returns></returns>
        public IEnumerable<NWFSchemeEntity> GetSchemePageList(Pagination pagination, string schemeInfoId)
        {
            return nWFSchemeService.GetSchemePageList(pagination, schemeInfoId);
        }
        /// <summary>
        /// 获取模板的实体
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        public NWFSchemeEntity GetSchemeEntity(string keyValue)
        {
            return nWFSchemeService.GetSchemeEntity(keyValue);
        }


        /// <summary>
        /// 获取模板的实体
        /// </summary>
        /// <param name="schemeInfoId">流程信息主键</param>
        /// <returns></returns>
        public NWFSchemeEntity GetSchemeEntityBySchemeInfoId(string schemeInfoId)
        {
            return nWFSchemeService.GetSchemeEntityBySchemeInfoId(schemeInfoId);
        }

        /// <summary>
        /// 获取模板的实体
        /// </summary>
        /// <param name="schemeInfoId">流程信息主键</param>
        /// <returns></returns>
        public IEnumerable<NWFSchemeEntity> GetNewSchemeEntityBySchemeInfoId(string schemeInfoId)
        {
            return nWFSchemeService.GetNewSchemeEntityBySchemeInfoId(schemeInfoId);
        }

        #endregion

        #region 提交数据
        /// <summary>
        /// 虚拟删除模板信息
        /// </summary>
        /// <param name="keyValue">主键</param>
        public void DeleteEntity(string keyValue)
        {
            nWFSchemeService.DeleteEntity(keyValue);
        }
        /// <summary>
        /// 保存模板信息
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="infoEntity">模板基础信息</param>
        /// <param name="schemeEntity">模板信息</param>
        /// <param name="authList">模板权限信息</param>
        public void SaveEntity(string keyValue, NWFSchemeInfoEntity infoEntity, NWFSchemeEntity schemeEntity, List<NWFSchemeAuthEntity> authList)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                NWFSchemeEntity oldNWFSchemeEntity = GetSchemeEntity(infoEntity.F_SchemeId);
                if (oldNWFSchemeEntity.F_Content == schemeEntity.F_Content && oldNWFSchemeEntity.F_Type == schemeEntity.F_Type)
                {
                    schemeEntity = null;
                }
            }
            nWFSchemeService.SaveEntity(keyValue, infoEntity, schemeEntity, authList);
        }

        /// <summary>
        /// 保存模板信息 add by zhuxing
        /// </summary>
        /// <param name="currentSchemeInfo">当前模板数据</param>
        /// <param name="commonScheme">最新通用模板数据</param>
        public void SaveEntity(NWFSchemeInfoEntity currentSchemeInfo, NWFSchemeEntity commonScheme)
        {
            nWFSchemeService.SaveEntity(currentSchemeInfo, commonScheme);
        }
        /// <summary>
        /// 保存流程分类
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="entity">流程分类实体</param>
        public void CategorySaveEntity(string keyValue, NWFSchemeCategoryEntity entity)
        {

            nWFSchemeService.CategorySaveEntity(keyValue, entity);
        }

        /// <summary>
        /// 删除流程分类
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="entity">流程分类实体</param>
        public void CategoryDeleteEntity(string keyValue)
        {

            nWFSchemeService.CategoryDeleteEntity(keyValue);
        }
        /// <summary>
        /// 更新流程模板
        /// </summary>
        /// <param name="schemeInfoId">模板信息主键</param>
        /// <param name="schemeId">模板主键</param>
        public void UpdateScheme(string schemeInfoId, string schemeId)
        {
            nWFSchemeService.UpdateScheme(schemeInfoId, schemeId);
        }
        /// <summary>
        /// 更新自定义表单模板状态
        /// </summary>
        /// <param name="schemeInfoId">模板信息主键</param>
        /// <param name="state">状态1启用0禁用</param>
        public void UpdateState(string schemeInfoId, int state)
        {
            nWFSchemeService.UpdateState(schemeInfoId, state);
        }

        /// <summary>
        /// 复制流程模板 
        /// </summary>
        /// <param name="ProjectId">项目ID</param>
        public void CopyScheme(string ProjectId)
        {
            string PLANTTYPE_PROJECT = ConstantBiz.PLANTTYPE_PROJECT;
            UserInfo userInfo = LoginUserInfo.Get();
            //获取流程分类数据，类别为00002项目平台,项目Id为空
            List<NWFSchemeCategoryEntity> schemeCategoryList = GetNWFSchemeCategoryEntityList(PLANTTYPE_PROJECT, null);
            if (schemeCategoryList.Count > 0)
            {
                foreach (var schemeCategory in schemeCategoryList)
                {
                    //获取流程，类别为00002项目平台,项目ID
                    List<NWFSchemeCategoryEntity> categoryInfoList_proj = GetNWFSchemeCategoryEntityList(PLANTTYPE_PROJECT, ProjectId);
                    NWFSchemeCategoryEntity nWFSchemeCategoryEntity = categoryInfoList_proj.Find(p => p.ProjectId == ProjectId && p.PlantType == PLANTTYPE_PROJECT && p.ClassifyName == schemeCategory.ClassifyName);
                    if (nWFSchemeCategoryEntity == null)
                    {
                        nWFSchemeCategoryEntity = new NWFSchemeCategoryEntity();
                        nWFSchemeCategoryEntity.ProjectId = ProjectId;
                        nWFSchemeCategoryEntity.PlantType = PLANTTYPE_PROJECT;
                        nWFSchemeCategoryEntity.ClassifyName = schemeCategory.ClassifyName;
                        nWFSchemeCategoryEntity.CreateUserID = userInfo.userId;
                        nWFSchemeCategoryEntity.CreateTime = DateTime.Now;
                        CategorySaveEntity("", nWFSchemeCategoryEntity);
                    }

                    //根据分类Id，获取流程模板，项目Id为空的数据
                    List<NWFSchemeInfoEntity> nWFSchemeInfoList = (List<NWFSchemeInfoEntity>)GetInfoListByCategoryId(schemeCategory.FlowClassifyID);
                    if (nWFSchemeInfoList.Count > 0)
                    {
                        foreach (var schemeInfo in nWFSchemeInfoList)
                        {
                            if (GetInfoEntityByCodeAndProjectId(schemeInfo.F_Code, ProjectId) == null)
                            {
                                NWFSchemeInfoEntity schemeInfoEntity = schemeInfo;
                                NWFSchemeEntity schemeEntity = GetSchemeEntity(schemeInfoEntity.F_SchemeId);
                                var nWFSchemeAuthList = (List<NWFSchemeAuthEntity>)GetAuthList(schemeInfoEntity.F_Id);
                                schemeInfoEntity.ProjectId = ProjectId;
                                schemeInfoEntity.F_Category = nWFSchemeCategoryEntity.FlowClassifyID;
                                schemeEntity.F_Type = 1;
                                SaveEntity("", schemeInfoEntity, schemeEntity, nWFSchemeAuthList);
                            }
                        }
                    }
                }
            }
        }
        #endregion
    }
}
